The PDFDocument
and PDFPage
classes in the Scripting app provide a simplified and powerful interface for working with PDF files, including reading, editing, and exporting content. Both synchronous and asynchronous methods are supported for optimal flexibility.
PDFPage
ClassRepresents a single page within a PDF document. It offers access to text content, raw data, and related metadata.
PDFPage.fromImage(image: UIImage): PDFPage | null
Creates a new PDF page from the given image.
Parameters:
image
: The image to convert into a PDF page.Returns: A PDFPage
instance, or null
if creation fails.
document: PDFDocument | null
The parent PDFDocument
instance this page belongs to, or null
if it hasn’t been added to a document yet.
label: string | null
A user-visible label for the page, such as a page title.
numberOfCharacters: number
The total number of text characters on the page.
string: Promise<string | null>
Returns the text content of the page. May return null
if the page contains only images or non-text content.
data: Promise<Data | null>
Returns the raw binary representation of the page.
PDFDocument
ClassRepresents an entire PDF document. This class enables reading, inspecting, editing pages, and saving the document.
PDFDocument.fromData(data: Data): PDFDocument | null
Creates a new document from raw PDF data.
Parameters:
data
: A valid PDF binary buffer.Returns: A PDFDocument
instance or null
if the data is invalid.
PDFDocument.fromFilePath(filePath: string): PDFDocument | null
Loads a PDF document from a file path.
Parameters:
filePath
: Path to a valid PDF file.Returns: A PDFDocument
instance, or null
if the file cannot be read.
pageCount: number
The total number of pages in the document.
filePath: string | null
The original file path of the PDF, or null
if created in memory.
isLocked: boolean
Indicates whether the document is locked and requires a password.
isEncrypted: boolean
Indicates whether the document is encrypted.
documentAttributes: object | null
Optional document metadata:
data: Promise<Data | null>
Asynchronously retrieves the document's binary data.
string: Promise<string | null>
Asynchronously retrieves the full text content of the PDF. May return null
for image-based documents.
pageAt(index: number): PDFPage | null
Returns the page at the given index.
Parameters:
index
: Zero-based index of the page.Returns: A PDFPage
instance or null
if out of range.
indexOf(page: PDFPage): number
Returns the index of the specified page within the document.
Parameters:
page
: A PDFPage
object from this document.Returns: The page index, or -1
if not found.
removePageAt(index: number): void
Removes the page at the specified index.
insertPageAt(page: PDFPage, atIndex: number): void
Inserts a page at the specified index.
exchangePage(atIndex: number, withPageIndex: number): void
Swaps two pages in the document.
writeSync(toFilePath: string, options?): boolean
Writes the document to a file synchronously with optional encryption and configuration.
Parameters:
toFilePath
: Path to save the new PDF.
options
(optional):
Returns: true
if the file was saved successfully, false
otherwise.
write(toFilePath: string, options?): Promise<boolean>
Asynchronously writes the document to a file.
writeSync
.Promise<boolean>
indicating success.unlock(password: string): boolean
Attempts to unlock an encrypted PDF document.
Parameters:
password
: The password string.Returns: true
if unlocked successfully, otherwise false
.